﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using cecbData;

namespace CECB
{
    /// <summary>
    /// Interaction logic for MetrixResultsCharts.xaml
    /// </summary>
    public partial class MetrixResultsCharts : Window
    {
        public MetrixResultsCharts()
        {
            InitializeComponent();

        }


        public MetrixResultsCharts(String projecReference)
        {

            InitializeComponent();
            showColumnChart(projecReference);
        }



        double sum = 0;
        DSSEntities cecbContext = new DSSEntities();

        private void showColumnChart(String projectReference)
        {

            // get the summation
            var quary = (from c in cecbContext.SimpleMatrixValues
                         where c.proj_reference.Contains(projectReference)

                         group c by new
                         {
                             c.proj_reference
                         }
                             into cc
                             select cc.Sum(c => c.mtrx_value)).FirstOrDefault();

            sum = quary.Value;

            //get phisical afection
            double presentage = 0;
            var quary1 = (from c in cecbContext.SimpleMatrixValues
                          join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                          join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                          where c.proj_reference.Contains(projectReference)
                          && g.cmpt_mainCat.Contains("Physical Environment")

                          group c by new
                          {
                              c.proj_reference
                          }

                              into cc
                              select cc.Sum(c => c.mtrx_value) / sum).FirstOrDefault();

            try
            {
                presentage = quary1.Value;
            }
            catch (Exception)
            {

                if (presentage == null)
                    presentage = 0.00;
                else
                    presentage = Math.Round(presentage, 2) * 100;
            }





            //get sociological Affection
            double presentage2 = 0;
            var quary2 = (from c in cecbContext.SimpleMatrixValues
                          join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                          join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                          where c.proj_reference.Contains(projectReference)
                          && g.cmpt_mainCat.Contains("Social Environmental")

                          group c by new
                          {
                              c.proj_reference
                          }

                              into cc
                              select cc.Sum(c => c.mtrx_value) / sum).FirstOrDefault();


            try
            {
                presentage2 = quary2.Value;
            }
            catch (Exception)
            {

                if (presentage2 == null)
                    presentage2 = 0.00;
                else
                    presentage2 = Math.Round(presentage2, 2) * 100;
            }



            //get Echological Affection

            double presentage3 = 0;
            var quary3 = (from c in cecbContext.SimpleMatrixValues
                          join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                          join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                          where c.proj_reference.Contains(projectReference)
                          && g.cmpt_mainCat.Contains("Ecological Environmental")

                          group c by new
                          {
                              c.proj_reference
                          }

                              into cc
                              select cc.Sum(c => c.mtrx_value) / sum).FirstOrDefault();

            try
            {
                presentage3 = quary3.Value;
            }
            catch (Exception)
            {

                if (presentage3 == null)
                    presentage3 = 0.00;
                else
                    presentage3 = Math.Round(presentage3, 2) * 100;
            }



            List<KeyValuePair<string, double>> valueList = new List<KeyValuePair<string, double>>();
            valueList.Add(new KeyValuePair<string, double>("Physical Environment", presentage));
            valueList.Add(new KeyValuePair<string, double>("Social Environmental", presentage2));
            valueList.Add(new KeyValuePair<string, double>("Ecological Environmental", presentage3));

            //Setting data for column chart
            columnChart.DataContext = valueList;

            // Setting data for pie chart
            pieChart.DataContext = valueList;

            //Setting data for area chart
            areaChart.DataContext = valueList;

            //Setting data for bar chart
            barChart.DataContext = valueList;

            //Setting data for line chart
            lineChart.DataContext = valueList;
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }


    }
}
